<?php

namespace app\admin\model;

use think\Model;


class Item extends Model
{

    

    

    // 表名
    protected $name = 'item';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'tag_text',
        'status_text'
    ];
    

    
    public function getTagList()
    {
        return ['1' => __('Tag 1'), '2' => __('Tag 2'), '3' => __('Tag 3'), '4' => __('Tag 4'), '5' => __('Tag 5')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '-1' => __('Status -1')];
    }


    public function getTagTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tag']) ? $data['tag'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getTagList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setTagAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function category()
    {
        return $this->belongsTo('Category', 'category_id')->setEagerlyType(0);
    }
}
